#!/usr/bin/env python3
"""
智能MCP系统启动脚本
"""

import asyncio
import uvicorn
import logging
import os
import sys
from pathlib import Path

# 添加项目根目录到Python路径
project_root = Path(__file__).parent
sys.path.insert(0, str(project_root))

from agent_mcp.app.main_app import create_app
from agent_mcp.cores.config import setup_logging, DEFAULT_HOST, DEFAULT_PORT

def main():
    """主函数"""
    print("=" * 60)
    print("🤖 智能MCP系统启动中...")
    print("=" * 60)
    
    # 设置日志
    setup_logging()
    logger = logging.getLogger("mcp_server")
    
    # 检查环境变量
    required_env_vars = [
        "LLM_API_KEY",
        "KYLIN_DEFAULT_SERVER"
    ]
    
    missing_vars = []
    for var in required_env_vars:
        if not os.environ.get(var):
            missing_vars.append(var)
    
    if missing_vars:
        print("⚠️  警告: 以下环境变量未设置:")
        for var in missing_vars:
            print(f"   - {var}")
        print("\n请设置这些环境变量以确保系统正常运行。")
        print("您可以在 .env 文件中设置这些变量。")
    
    # 创建应用
    app = create_app()
    
    # 获取配置
    host = os.environ.get("HOST", DEFAULT_HOST)
    port = int(os.environ.get("PORT", DEFAULT_PORT))
    
    print(f"🌐 服务器地址: http://{host}:{port}")
    print(f"📊 API文档: http://{host}:{port}/docs")
    print(f"🔧 健康检查: http://{host}:{port}/api/health")
    print("=" * 60)
    
    # 启动服务器
    try:
        uvicorn.run(
            app,
            host=host,
            port=port,
            log_level="info",
            reload=False
        )
    except KeyboardInterrupt:
        print("\n🛑 服务器已停止")
    except Exception as e:
        logger.error(f"启动失败: {e}")
        print(f"❌ 启动失败: {e}")

if __name__ == "__main__":
    main() 